package com.sikao.app.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.sikao.app.app.MyApplication;

import java.util.UUID;

/**
 * Created by y50 on 2016/4/20.
 */
public class EquipmentUtils {

	private static String equipmentId;

	public static String getVersionName() {
		PackageManager manager = MyApplication.getInstance().getPackageManager();
		PackageInfo info = null;
		try {
			info = manager.getPackageInfo(MyApplication.getInstance().getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
			return "1.0.0";
		}
	}

	public static int getVersionCode() {
		PackageManager manager = MyApplication.getInstance().getPackageManager();
		PackageInfo info = null;
		try {
			info = manager.getPackageInfo(MyApplication.getInstance().getPackageName(), 0);
			int version = info.versionCode;
			return version;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
			return 1;
		}
	}

	public static String getDeviceId(Context context) {

		if (equipmentId != null && !equipmentId.isEmpty()) {
			return equipmentId;
		}

		StringBuilder deviceId = new StringBuilder();
		// 渠道标志
		deviceId.append("a");

		try {

			//IMEI（imei）
			TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			String imei = tm.getDeviceId();
			if (!TextUtils.isEmpty(imei)) {
				deviceId.append("imei");
				deviceId.append(imei);
				equipmentId = deviceId.toString();
				return deviceId.toString();
			}
			//wifi mac地址
			WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = wifi.getConnectionInfo();
			String wifiMac = info.getMacAddress();
			if (!TextUtils.isEmpty(wifiMac)) {
				deviceId.append("wifi");
				deviceId.append(wifiMac);
				equipmentId = deviceId.toString();
				return deviceId.toString();
			}

			//序列号（sn）
			String sn = tm.getSimSerialNumber();
			if (!TextUtils.isEmpty(sn)) {
				deviceId.append("sn");
				deviceId.append(sn);
				equipmentId = deviceId.toString();
				return deviceId.toString();
			}

			//如果上面都没有， 则生成一个id：随机码
			String uuid = getUUID(context);
			if (!TextUtils.isEmpty(uuid)) {
				deviceId.append("id");
				deviceId.append(uuid);
				equipmentId = deviceId.toString();
				return deviceId.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			deviceId.append("id").append(getUUID(context));
		}

		equipmentId = deviceId.toString();

		return equipmentId;
	}


	/**
	 * 得到全局唯一UUID
	 */
	public static String getUUID(Context context) {
		SharepreferenceUtil sp = SharepreferenceUtil.getInstance(context);
		String uuid = null;
		if (sp != null) {
			uuid = sp.getString("uuid");
		}

		if (uuid == null || uuid.isEmpty()) {
			uuid = UUID.randomUUID().toString();
			sp.putString("uuid", uuid);
		}
		return uuid;
	}
}